package fi.solita.dojo.mazeserver.map

import fi.solita.dojo.mazeserver.util._

object Turn extends Enumeration {
	type Turn = Value
	val Front = Value("f")
	val Right = Value("r")
	val Left = Value("l")
	val Back = Value("b")
}

object Directions extends Enumeration {
	type Direction = Value
	val North = Value("N")
	val East = Value("E")
	val South = Value("S")
	val West = Value("W")

    def randomDirection(): Directions.Direction = {
      val i = Util.randomPositiveInt(4)
      i match {
        case 1 => Directions.North
        case 2 => Directions.East
        case 3 => Directions.South
        case 0 => Directions.West
      }
    }
 
	def turn(t: Turn.Turn, d: Direction): Direction = t match {
	case Turn.Right => right(d)
	case Turn.Left => left(d)
	case Turn.Back => opposite(d)
	case _ => d
	}

	def right(d: Direction) = d match {
	case North => East
	case East => South
	case South => West
	case West => North
	case _ => d
	}

	def left(d: Direction) = d match {
	case North => West
	case East => North
	case South => East
	case West => South
	case _ => d 
	}

	def opposite(d: Direction) = d match {
	case North => South
	case East => West
	case South => North
	case West => East
	case _ => d 
	}
}

case class Coordinates (row:  Int, column:  Int){

	override def toString() = row.toString + " " + column.toString  

	def neighbour(direction: Directions.Direction): Coordinates = {
		direction match {
		case Directions.North => {Coordinates(row - 1, column)}
		case Directions.East => {Coordinates(row, column + 1)}
		case Directions.South => {Coordinates(row + 1, column)}
		case Directions.West => {Coordinates(row, column - 1)}
		}
	}

	def - (that: Coordinates) = {
		Coordinates(this.row - that.row, this.column - that.column)
	}

	def + (that: Coordinates) = {
		Coordinates(this.row + that.row, this.column + that.column)
	}

}
